package com.finalcial.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.finalcial.model.Company;
import com.finalcial.po.LoginMsg;
import com.finalcial.po.UserPersonalMsg;
import com.finalcial.service.CompanyService;
import com.finalcial.service.DeptLevelService;
import com.finalcial.service.UserService;
import com.finalcial.util.BankUtil;
import com.finalcial.util.MD5Util;
import com.finalcial.util.PageUtil;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private DeptLevelService deptLevelService;
	@Autowired
	private CompanyService companyService;
	
	/**
	 * 对应管理员--个人信息界面(所有用户)
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getUserMsg")
	public String getUserMsg(HttpServletRequest request, HttpServletResponse response) {
		String pageNow = request.getParameter("pageNow");
		if(pageNow == null || pageNow.endsWith("0") == true) {
			pageNow = "1";
		}
		PageUtil page = null;
		List<Map<String, Object>> userMsglist = null;
		Integer totalCount = userService.getUserCounts();
		page = new PageUtil(totalCount, Integer.parseInt(pageNow));
		if(page.getTotalPageCount() < Integer.parseInt(pageNow)) {
			page.setPageNow(page.getTotalPageCount());
		}
		userMsglist = userService.listEmployeeInformation(page.getStartPos(), page.getPageSize());
		for(Map<String, Object> map:userMsglist) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}
		request.setAttribute("page", page);
		request.setAttribute("userMsglist", userMsglist);
		return "systemsetting/userInfo";
	}
	
	/**
	 * 对应非管理员--个人信息界面(单用户)
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getInformationByOrdinary")
	public String getEmployeeInformationByOrdinary(HttpServletRequest request, HttpServletResponse response) {
		LoginMsg loginMsg = (LoginMsg) request.getSession().getAttribute("LoginMsg");
		Integer userId = loginMsg.getUserId();
		UserPersonalMsg userPersonalMsg = userService.getEmployeeInformationByOrdinary(userId);
		request.setAttribute("userPersonalMsg", userPersonalMsg);
		return "systemsetting/personalInfo";
	}
	/**
	 * 个人用户界面修改信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/updateUserInfoOne")
	public Map<String, Object> updateUserInfoOne(HttpServletRequest request, HttpServletResponse response){
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");
		
		
		String repassword = request.getParameter("repassword");
		Map<String, Object> map = null;
		if(password.endsWith(repassword) == true) {
			map = new HashMap<String, Object>();
			Integer num = userService.updateUserInfoOne(Integer.valueOf(userId), MD5Util.convertMD5(password));
			UserPersonalMsg userPersonalMsg = userService.getEmployeeInformationByOrdinary(Integer.valueOf(userId));
			String msg = "更新失败！";
			if(num != 0) {
				msg = "更新成功！";
			}
			map.put("msg", msg);
			map.put("userPersonalMsg", userPersonalMsg);
		}
		return map;
	}
	
	
	/**
	 * 管理面-查看用户页面点击删除后，通过ajax删除该用户并显示其它所有用户
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteEmployeeInformation", method = RequestMethod.POST)
	public Map<String, Object> deleteEmployeeInformation(HttpServletRequest request, HttpServletResponse response){
		String userId = request.getParameter("userId");
		//获得当前页面
		String pageNow = request.getParameter("pageNow");
		if(pageNow == null || pageNow.endsWith("0") == true) {
			pageNow = "1";
		}
		Integer num = userService.deleteEmployeeInformation(Integer.parseInt(userId));
		String msg = "删除失败！";
		if(num != 0) {
			msg = "删除成功！";
		}
		Map<String, Object> map = new HashMap<String, Object>();
		PageUtil page = null;
		List<Map<String, Object>> userMsglist = null;
		//获得用户总记录数
		Integer totalCount = userService.getUserCounts();
		page = new PageUtil(totalCount, Integer.parseInt(pageNow));
		if(page.getTotalPageCount() < Integer.parseInt(pageNow)) {
			page.setPageNow(page.getTotalPageCount());
		}
		userMsglist = userService.listEmployeeInformation(page.getStartPos(), page.getPageSize());
		map.put("num", num);
		map.put("page", page);
		map.put("msg", msg);
		map.put("userMsglist", userMsglist);
		return map;
	}
	/**
	 * ajax请求，更新用户信息
	 * @param request
	 * @param response
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/updateEmployeeInformation", method = RequestMethod.POST)
	public Map<String, Object> updateEmployeeInformation(HttpServletRequest request, HttpServletResponse response) throws JsonParseException, JsonMappingException, IOException{
		String params = request.getParameter("params");
		ObjectMapper mapper = new ObjectMapper();
		//解析json数据格式，解析为map
		Map<String,Object> mm = mapper.readValue(params, Map.class);
		String userId = (String) mm.get("userId");
		String departmentName = (String) mm.get("departmentName");
		String userTypeName = (String) mm.get("userTypeName");
		String companyName = (String) mm.get("companyName");
		String phone = (String) mm.get("phone");
		String bankNumber = (String) mm.get("bankNumber");
		String password = (String) mm.get("password");
		//再次输入密码
		String repassword = (String) mm.get("repassword");
		String msg = "更新成功！";
		System.out.println(password +"-----0-----"+repassword);
		Map<String, Object> map = null;
		Integer num = null;
		if(password.endsWith(repassword) == true) {
			//获得当前页面
			String pageNow = request.getParameter("pageNow");
			if(pageNow == null || pageNow.endsWith("0") == true) {
				pageNow = "1";
			}
			Integer departmentId = deptLevelService.getDeptNameById(departmentName);
			Integer userTypeId = deptLevelService.getDeptUserTypeId(departmentId, userTypeName);
			Company company = companyService.getCompanyByName(companyName);
			String bankName = BankUtil.getNameOfBank(bankNumber);
			num = userService.updateEmployeeInformation(Integer.parseInt(userId), departmentId, userTypeId, company.getCompanyid(), phone, bankNumber, bankName, MD5Util.convertMD5(password));
			if(num != 1) {
				msg = "更新失败！";
			}
			map = new HashMap<String, Object>();
			PageUtil page = null;
			List<Map<String, Object>> userMsglist = null;
			//获得用户总记录数
			Integer totalCount = userService.getUserCounts();
			//页数
			page = new PageUtil(totalCount, Integer.parseInt(pageNow));
			if(page.getTotalPageCount() < Integer.parseInt(pageNow)) {
				page.setPageNow(page.getTotalPageCount());
			}
			//获得用户记录
			userMsglist = userService.listEmployeeInformation(page.getStartPos(), page.getPageSize());
			map.put("page", page);
			map.put("userMsglist", userMsglist);
		}
		map.put("num", num);
		map.put("msg", msg);
		return map;
	}
	/**
	 * ajax请求，新增用户信息
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insertEmployeeInformation", method = RequestMethod.POST)
	public Map<String, Object> insertEmployeeInformation(HttpServletRequest request, HttpServletResponse response){
		String userName = request.getParameter("userName");
		String departmentName = request.getParameter("departmentName");
		String userTypeName = request.getParameter("userTypeName");
		String companyName = request.getParameter("companyName");
		String phone = request.getParameter("phone");
		String bankNumber = request.getParameter("bankNumber");
		String password = request.getParameter("password");
		//再次输入密码
		String repassword = request.getParameter("repassword");
		String msg = "两次密码不一致";
		Map<String, Object> map = null;
		PageUtil page = null;
		List<Map<String, Object>> userMsglist = null;
		if(password.endsWith(repassword) == true) {
			//获得当前页面
			String pageNow = request.getParameter("pageNow");
			if(pageNow == null || pageNow.endsWith("0") == true) {
				pageNow = "1";
			}
			Integer departmentId = deptLevelService.getDeptNameById(departmentName);
			Integer userTypeId = deptLevelService.getDeptUserTypeId(departmentId, userTypeName);
			Company company = companyService.getCompanyByName(companyName);
			String bankName = BankUtil.getNameOfBank(bankNumber);
			Integer num = userService.insertEmployeeInformation(userName,departmentId, userTypeId, company.getCompanyid(), phone, bankNumber, bankName, MD5Util.convertMD5(password));
			if(num != 0) {
				msg = "插入成功！";
			}
			map = new HashMap<String, Object>();
			//获得用户总记录数
			Integer totalCount = userService.getUserCounts();
			//页数
			page = new PageUtil(totalCount, Integer.parseInt(pageNow));
			if(page.getTotalPageCount() < Integer.parseInt(pageNow)) {
				page.setPageNow(page.getTotalPageCount());
			}
			//获得用户记录
			userMsglist = userService.listEmployeeInformation(page.getStartPos(), page.getPageSize());
			map.put("page", page);
			map.put("msg", msg);
			map.put("userMsglist", userMsglist);
		}
		return map;
	}
	
	/**
	 * ajax请求，多条件查询用户信息
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getEmployeeInformation", method = RequestMethod.POST)
	public Map<String, Object> getEmployeeInformation(HttpServletRequest request, HttpServletResponse response){
		String userId = request.getParameter("userId");
		String userName = request.getParameter("userName");
		String departmentName = request.getParameter("departmentName");
		String userTypeName = request.getParameter("userTypeName");
		String companyName = request.getParameter("companyName");
		//获得当前页面
		String pageNow = request.getParameter("pageNow");
		if(pageNow == null) {
			pageNow = "1";
		}
		List<Map<String, Object>> userMsglist = null;
		if(userId == "" || userId == null) {
			userMsglist = userService.getEmployeeInformation(null,userName,departmentName, userTypeName, companyName);
		}else {
			userMsglist = userService.getEmployeeInformation(Integer.valueOf(userId),userName,departmentName, userTypeName, companyName);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		/*PageUtil page = null;
		//获得用户总记录数
		Integer totalCount = userService.getUserCounts();
		//页数
		page = new PageUtil(totalCount, Integer.parseInt(pageNow));*/
		map.put("userMsglist", userMsglist);
		return map;
	}
}
